/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysLoginDao.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月22日
 */
package com.sirdc.modules.sys.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @version Revision: 0.0.1
 * @author: huiyang.yu
 * @Date: 2015年1月22日
 */
@Repository
public class SysLoginDao extends StringDao<SysLogin> {


    /**
     * 查看全部的用户登陆信息
     *
     * @param filter
     * @return
     */
    public List<SysLogin> query(SysLoginFilter filter) {
        GenericQuery query = createQuery("obj");
        return query.listResult(filter);
    }

    /**
     * @param token
     * @return
     */
    public SysLogin getUserByLoginName(String token) {
        String hql = "from SysLogin login where login.username=:username";
        GenericQuery query = create(hql);
        query.setParam("username", token);
        return query.singleResult();
    }

    /**
     * @param mail
     * @return
     */
    public SysLogin getLoginByEmail(String mail) {
        String hql = "from SysLogin login where login.email=:email";
        GenericQuery query = create(hql);
        query.setParam("email", mail);
        return query.singleResult();
    }

    /**
     * @param mobile
     * @return
     */
    public SysLogin getLoginByMobile(String mobile) {
        String hql = "from SysLogin login where login.mobile=:mobile";
        GenericQuery query = create(hql);
        query.setParam("mobile", mobile);
        return query.singleResult();
    }

    /**
     * @param userId
     * @return
     */
    public SysLogin getLoginByUserId(String userId) {
        String hql = "from SysLogin login where login.sysId=:userId";
        GenericQuery query = create(hql);
        query.setParam("userId", userId);
        return query.singleResult();
    }

    /**
     * @param filter
     * @return
     */
    public List<String> queryLoginByRoleDept(SysUserFilter filter) {
        GenericQuery query = create("select sysId from SysLogin where roleId=:roleId and deptId=:deptId");
        query.setParam("roleId", filter.getRoleId());
        query.setParam("deptId", filter.getDeptId());
        return query.listResult();
    }

    /**
     * @param userId
     * @param roleId
     * @param deptId
     * @return
     */
    public boolean isExistUserPermission(String userId, String roleId, String deptId) {
        GenericQuery query = create("from SysLogin where roleId=:roleId and deptId=:deptId and sysId=:userId");
        query.setParam("roleId", roleId);
        query.setParam("deptId", deptId);
        query.setParam("userId", userId);
        return (query.count() > 0);
    }

    /**
     * @param sno
     * @return
     * @author: huiyang.yu
     */
    public SysLogin getLoginBySno(String sno) {
        String hql = "from SysLogin login where login.sno=:sno";
        GenericQuery query = create(hql);
        query.setParam("sno", sno);
        return query.singleResult();
    }

    public SysLogin getLoginByUsername(String username) {
        String hql = "from SysLogin login where login.username=:username";
        GenericQuery query = create(hql);
        query.setParam("username", username);
        return query.singleResult();
    }
}
